package com.hmdp.config;

import com.hmdp.utils.LoginInterceptor;
import com.hmdp.utils.RefreshToeknInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    //配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登录拦截器
        registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns("/shop/**",
                                    "/voucher/**",
                                    "/shop-type/**",
                                    "/upload/**",
                                    "/blog/hot",
                                    "/user/code",
                                    "/user/login"
                                    ).order(1);
       //由于RefreshToeknInterceptor这个token刷新拦截器是我们自定义的,不会被spring自动做依赖注入,
       //所以StringRedisTemple无法用@注解直接在拦截器里面注入,所以我们在配置类MvcConfig中先依赖注入后通过传参传过去给拦截器用
        registry.addInterceptor(new RefreshToeknInterceptor(stringRedisTemplate))
                .addPathPatterns("/**")
                .excludePathPatterns("/login")
                .excludePathPatterns("/user/code")
                .order(0); //token刷新时间拦截器(需要优先执行,用order配置,数值越低越先执行)

    }
}
